from flatfielddefinition import FlatFieldDefinition

class FlatFileLayout:
    def __init__(self):
        self._flat_field_list = []

    def addFieldObject(self, passed_FlatFieldDefinition):
        assert isinstance(passed_FlatFieldDefinition, FlatFieldDefinition)
        self._flat_field_list.append(passed_FlatFieldDefinition)

    def addFieldByIndex(self, first_index, last_index, key_name=''):
        assert isinstance(first_index, int)
        assert isinstance(last_index, int)
        assert isinstance(key_name, str)

        new_FlatFieldDefinition = FlatFieldDefinition(first_index, last_index, key_name)
        self.addFieldObject(new_FlatFieldDefinition)

    def addFieldByLength(self, length=1, key_name=''):
        assert isinstance(length, int)
        assert isinstance(key_name, str)

        target_first_index = self.getLastIndex() + 1
        target_last_index = target_first_index + length
        new_FlatFieldDefinition = FlatFieldDefinition(target_first_index, target_last_index, key_name)
        self.addFieldObject(new_FlatFieldDefinition)

    def getLastIndex(self):
        '''Return the [integer] last last_index of all this object's defined fields, or -1 if no fields defined'''
        highest_index = -1 #This will track the highest last_index found so far.
        for field in self._flat_field_list:
            this_last_index = field.getLastIndex() #Reducing the number of function calls should improve performance.
            if this_last_index > highest_index:
                highest_index = this_last_index
        return highest_index

    def getFirstIndex(self):
        return 0 #Not yet implemented.

    def getCreateTableSQL(self):
        '''Return SQL statements necessary to create a table matching this layout.'''
        #Wouldn't this be neat?
        return "-- Not yet implemented. :("
